package io.github.weiranyi;


import io.github.weiranyi.dao.UserDao;
import io.github.weiranyi.entity.User;
import io.github.weiranyi.service.UserService;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

public class test {
    // 模块化拆分------------------------------------------
    @Test
    public void test10_model() throws Exception {
        /* 多模块引入方式1："spring-service.xml","spring-dao.xml"
        *  ClassPathXmlApplicationContext classPathXmlApplicationContext =
        *        new ClassPathXmlApplicationContext("spring-service.xml","spring-dao.xml");
        *
        * */
        ClassPathXmlApplicationContext classPathXmlApplicationContext =
                new ClassPathXmlApplicationContext("spring.xml");
        UserDao userDao = classPathXmlApplicationContext.getBean("userDao4",UserDao.class);
        userDao.save();
        classPathXmlApplicationContext.close();// 手动销毁方法，不写这个就来不及打印
    }
    // 数据类型注入的回顾------------------------------------------
    @Test
    public void test9_userDao() throws Exception {
        ClassPathXmlApplicationContext classPathXmlApplicationContext =
                new ClassPathXmlApplicationContext("spring.xml");
        UserDao userDao = classPathXmlApplicationContext.getBean("userDao4",UserDao.class);
        userDao.save();
        classPathXmlApplicationContext.close();// 手动销毁方法，不写这个就来不及打印
    }
    @Test
    public void test8_DI_data() throws Exception {
        ClassPathXmlApplicationContext classPathXmlApplicationContext =
                new ClassPathXmlApplicationContext("spring.xml");
        User user = classPathXmlApplicationContext.getBean("user", User.class);
        System.out.println(user);
    }
    // ---------------------------------------------------------
    @Test
    public void test7_DI_constructor_setter() throws Exception {
        ClassPathXmlApplicationContext classPathXmlApplicationContext =
                new ClassPathXmlApplicationContext("spring.xml");
        UserService userServiceImpl = (UserService) classPathXmlApplicationContext.getBean("UserService1");
        userServiceImpl.save();
    }

    @Test
    public void test6_DI_manual() throws Exception {
        ClassPathXmlApplicationContext classPathXmlApplicationContext =
                new ClassPathXmlApplicationContext("spring.xml");
        UserService userServiceImpl = (UserService) classPathXmlApplicationContext.getBean("UserService");
        userServiceImpl.save();
    }

    @Test
    public void test5_factoryMethod() throws Exception {
        ClassPathXmlApplicationContext classPathXmlApplicationContext =
                new ClassPathXmlApplicationContext("spring.xml");
        Object userDao = classPathXmlApplicationContext.getBean("userDao3");
        System.out.println(userDao);
        classPathXmlApplicationContext.close();// 手动销毁方法，不写这个就来不及打印
    }

    @Test
    public void test4_factoryStaticMethod() throws Exception {
        ClassPathXmlApplicationContext classPathXmlApplicationContext =
                new ClassPathXmlApplicationContext("spring.xml");
        Object userDao = classPathXmlApplicationContext.getBean("userDao2");
        System.out.println(userDao);
        classPathXmlApplicationContext.close();// 手动销毁方法，不写这个就来不及打印
    }

    @Test
    public void test3_life() throws Exception {
        // BeanFactory核心接口执行的时候不会创建bean对象；getBean的时候才会存到容器中
        ClassPathXmlApplicationContext classPathXmlApplicationContext =
                new ClassPathXmlApplicationContext("spring.xml");
        Object userDao = classPathXmlApplicationContext.getBean("userDao1");
        System.out.println(userDao);
        classPathXmlApplicationContext.close();// 手动销毁方法，不写这个就来不及打印
    }

    @Test
    public void test2_testSave() throws Exception {
        // BeanFactory核心接口执行的时候不会创建bean对象；getBean的时候才会存到容器中
        BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring.xml"));
        Object userDao = beanFactory.getBean("userDao");
        System.out.println(userDao);
    }

    @Test
    public void test1_testSave() throws Exception {
        // ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext();
        // 获取到了上下文对象，借助上下文对象获取IOC容器中的bean[加载配置文件->解析->获取里面的class->反射获取实例对象—>存到容器]
        ApplicationContext classPathXmlApplicationContext =
                new ClassPathXmlApplicationContext("spring.xml");
        // 1.[beanId]使用上下文从容器中获得bean对象
        Object userDao = classPathXmlApplicationContext.getBean("userDao");
        // 2.[class类型] 当容器中存在多个同类的的实例时会报错
        UserDao beanUserDao = classPathXmlApplicationContext.getBean(UserDao.class);
        // 3.[多个相同类型bean的情况，配合Id一起用]
        classPathXmlApplicationContext.getBean("userDao", UserDao.class);
    }
}

